<?php

namespace App\Admin\Controller\Rbac;

class User extends \App\Admin\Controller\Authed {
    
    public function index() {
        
        $uid = intval($this->request->get('uid'));
        
        $do = $this->request->get('do');
        
        if ($do == 'lock') {
            $locked = $this->request->get('locked');
            if ($locked) {
                $result = QF()->Admin()->unlockOne($uid);
            } else {
                $result = QF()->Admin()->lockOne($uid);
            }
            
            if ($result) {
                succ();
            } else {
                fail();
            }
        }
        
        $filter_username = urldecode(trim($this->request->get('filter_username')));
        
        if ($filter_username) {
            $filter_ids = QF()->Admin()->searchUids($filter_username);
            if ($filter_ids) {
                $filter['uid'] = ['in' => $filter_ids];
            } else {
                $filter['uid'] = -1;
            }
        }
        
        $count = QF()->Admin()->countAll($filter);
        
        /* 分页 */
        $pager = $this->_createPager($count);
        $this->assign('pager', $pager);
        
        $rows = QF()->Admin()->getAll($pager['offset'], $pager['length'], $filter);
        
        foreach ($rows as &$row) {
            $row['rolename'] = QF()->Rbac()->Role()->getTitles($row['role_ids']);
            
            $weixin_redirect = urlencode($this->request->serverLocation() . $this->createUrl('rbac/user', 'weixin_callback', ['uid' => $row['uid']]));
            $row['url'] = \Sys('site_root') . "/wx/login/qronly?redirect={$weixin_redirect}";
        }
        unset($row);
        
        $this->assign('rows', $rows);
    }
    
    public function edit() {
        
        $uid = intval($this->request->get('uid'));
        
        $detail = QF()->Admin()->get($uid);
        
        if ($this->request->isPost()) {
            
            $data = $this->request->get();
            
            if (!$data['role_ids']) {
                $data['role_ids'] = [];
            }
            
            if ($uid) {
                
                $result = QF()->Admin()->update($uid, $data);
            } else {
                
                if (trim($data['username']) == '') {
                    fail('请输入账户名');
                }
                
                $exist_user = QF()->Admin()->getUidByUserName($data['username']);
                if ($exist_user) {
                    fail('该账户名已存在');
                }
                
                if (!trim($data['password'])) {
                    fail('请输入密码');
                }
                
                $result = QF()->Admin()->add($data);
            }
            
            if ($result) {
                succ('', '', 2);
            } else {
                fail();
            }
        }
        
        $this->detail = $detail;
        
        $this->roles = QF()->Rbac()->Role()->getCheckedTree($detail['role_ids']);
    }

}